import axios from "./axios";
import { list } from "./config.json";

const http = {};

list.forEach((item) => {
  if (!item.path) return console.log("request path");
  // 修改接口名称
  const name = item.name || item.path.split("/").pop();
  const method = item.method || "post";
  switch (method) {
    case "post":
      http[name] = (params, options) => axios.post(item.path, params, options);
      break;
    case "put":
      http[name] = (params, options) => axios.put(item.path, params, options);
      break;
    case "get":
      http[name] = (params, options) => axios.get(item.path, { params, ...options });
      break;
    case "delete":
      http[name] = (params, options) => axios.delete(item.path, { params, ...options });
      break;
  }
});

http.changePassword = (oldPassword, newPassword, token) => {
  const path = "/api/admin/admin"; // 修改成您的修改密码的接口地址
  const data = {
    old_password: oldPassword,
    new_password: newPassword,
    user_token: token // 如果需要将 token 作为请求体的一部分，可以放在这里
  };
  return axios.put(path, data);
};

export default http;
